

/**
 * 渠道券页面
 */
Ext.define("PRO.shop.view.cdkey.shopCouponView",{
  extend: 'Ext.panel.Panel',
  border:0,
  layout:'card',
  alias: 'widget.shop.shopcouponview',
  autoScroll : true,
  initComponent: function(){
      var me = this;
      var data = me.getData();
      me.items=[
          {
              xtype:"form",
              border:0,
              itemId:'couponview',
              layout:'column',
              items:[
                  {
                      xtype:'box',
                      html:'产品',
                      margin:'20px 0',
                      columnWidth:1
                  },
                  {
                      xtype: 'cbbfield',
                      dataArray:data,
                      itemId:'product',
                      columnWidth:1,
                      listeners:{
                          change:function(view){
                              var me = this;
                              var productId = view.getValue();
                              JE.ajax({
                                  url:'/je/shoppingmall/bill/getBillSnapshot',
                                  params:{
                                      productId:productId,
                                  },
                                  success:function(res){
                                      var res = JSON.parse(res.responseText);
                                      if(res.success){
                                          var type = res.obj.productInfo.values.CP_CPTYPE_NAME;
                                          var userControllerText = me.up('[itemId=couponview]').getCmp('userControllerText');
                                          var topPanel = me.up('[itemId=couponview]').getCmp('topPanel');
                                          topPanel.nowType = type;
                                          if(type=='CRM'){
                                              topPanel.hide();
                                              userControllerText.hide();
                                          }else{
                                              topPanel.show();
                                              userControllerText.show();
                                          }
                                          JE.ajax({
                                              url:'/je/shoppingmall/voucher/getProductConfig',
                                              params:{
                                                  productId:productId,
                                                  limitNumbersInput:type=='CRM'?'':'20'
                                              },
                                              success:function(res){
                                                  var useTime = me.up('[itemId=couponview]').getCmp('useTime');
                                                  var useTimeStore=useTime.store;
                                                  var res = JSON.parse(res.responseText);
                                                  var data = [];
                                                  if(res.success){
                                                      Ext.each(res.obj.configParameters,function(item){
                                                          data.push({
                                                              text:item.configText,
                                                              code:item.schemeId
                                                          })
                                                      });
                                                      useTimeStore.loadData(data);
                                                  }

                                              }
                                          })
                                      }else{
                                          JE.alert(res.obj)
                                      }
                                  }
                              })


                          }
                      }
                  },
                  {
                      xtype:'box',
                      html:'人员选择器',
                      margin:'20px 0',
                      itemId:'userControllerText',
                      hidden:true,
                      columnWidth:1
                  },
                  {
                      xtype:'panel',
                      layout:'vbox',
                      border:0,
                      itemId:'topPanel',
                      columnWidth:1,
                      hidden:true,
                      defaults:{
                          width:'100%'
                      },
                      items:[{
                          xtype:'container',
                          height: 40,
                          margin:'0 20 10 0',
                          layout: {
                              type:'hbox',
                              pack:'end'
                          },
                          itemId:'containerview',
                          items:[
                          {
                              xtype:'box',
                              margin:'7 0 0 0',
                              html:'<i class="jeicon jeicon-minus-line" style="color:#DDDDDD;margin-left:5px;cursor:pointer;"></i>',
                              listeners:{
                                  afterRender:function(view){
                                      view.getEl().on('click',function(e){
                                          if(e.getTarget('.jeicon-minus-line',1)){
                                              var topPanel=me.getCmp('couponview').getCmp('topPanel');
                                              var containerview=topPanel.getCmp('containerview');
                                              var number=containerview.getCmp('number')
                                              var sliderfieldview=topPanel.getCmp('sliderfieldview');
                                              var val=number.getValue();
                                              var newVal=val-10;
                                              if(newVal<10)return;
                                              sliderfieldview.setValue(newVal);
                                              number.setValue(newVal);
                                          }
                                       });

                                  }
                              }
                          },{
                              xtype:'numberfield',
                              itemId:'number',
                              value:10,
                              width:50,
                              hideTrigger:true,
                              minValue:10,
                              maxValue:500,
                              step:10,
                              listeners:{
                                  change:function(field,newVal,oldVal){
                                      var topPanel=me.getCmp('couponview').getCmp('topPanel');
                                      var sliderfieldview=topPanel.getCmp('sliderfieldview');
                                      sliderfieldview.setValue(newVal);
                                  }
                              }
                          },{
                              xtype:'box',
                              margin:'7 0 0 0',
                              html:'<i class="jeicon jeicon-plus" style="color:#DDDDDD;margin-right:5px;cursor:pointer;"></i>',
                              listeners:{
                                  afterRender:function(view){
                                      view.getEl().on('click',function(e){
                                          if(e.getTarget('.jeicon-plus',1)){
                                              var topPanel=me.getCmp('couponview').getCmp('topPanel');
                                              var containerview=topPanel.getCmp('containerview');
                                              var number=containerview.getCmp('number')
                                              var sliderfieldview=topPanel.getCmp('sliderfieldview');
                                              var val=number.getValue();
                                              var newVal=val+10;
                                              sliderfieldview.setValue(newVal);
                                              number.setValue(newVal);
                                          }
                                       });

                                  }
                              }
                          }]
                      },{
                          xtype:'sliderfield',
                          itemId:'sliderfieldview',
                          cls:'je-pf-field-slider',//je-field-slider
                          margin:'0 10 40 15',
                          value:10,
                          increment:10,
                          height:10,
                          maxValue:500,
                          useTips: false,
                          autoShow:false,
                          listeners:{
                              change:function(slider,newValue,thumb,eOpts){
                                  var topPanel=me.getCmp('couponview').getCmp('topPanel');
                                  var containerview=topPanel.getCmp('containerview');
                                  var number=containerview.getCmp('number');
                                  number.setValue(newValue);
                                  var pfcase=slider.getEl().down('.pfcase');
                                  slider.tipTextEl.el.dom.innerText=newValue;
                                  var sliderthumb=slider.getEl().down('.x-slider-thumb');
                                  var sliderthumbStyle=(newValue/500)*100+"%";
                                  var pfslider=slider.getEl().down('.pfslider');
                                  var sliderthumbStyletip=((newValue/500)*100)+"%";
                                  slider.tipEl.setStyle({width:sliderthumbStyletip})
                              },
                              afterrender:function(field,eOpts){
                                  var sliderinner=this.getEl().down('.x-slider-inner');
                                  var xslider=this.getEl().down('.x-slider');
                                  var sliderthumb=this.getEl().down('.x-slider-thumb');
                                  sliderthumb.setStyle({'z-index':10000})
                                  var horz=this.getEl().down('.x-slider-horz');
                                  horz.setStyle({'pointer-events':'all'});
                                  field.tipEl = sliderinner.insertHtml('beforeEnd','<div class="pfcase"><div class="pfslider"></div><div class="tip-text pfcasedown">10</div></div>',true)
                                  field.tipTextEl = field.tipEl.down('.tip-text');
                                  field.tipEl.alignTo(field.thumbs[0].el, 'bc', [0,-10]);
                                  field.pfsliderEl=this.getEl().down('.pfslider');
                                  var pfcase = this.getEl().down('.pfcase');
                                  pfcase.setStyle({left:0,top:0});
                              }
                          }
                      }]
                  },
                  {
                      xtype:'box',
                      html:'使用时长',
                      margin:'20px 0',
                      columnWidth:1
                  },
                  {
                      xtype: 'cbbfield',
                      itemId:'useTime',
                      columnWidth:1
                  }
              ]
          },
          {
              xtype:"form",
              border:0,
              layout:'column',
              itemId:'subVoucher',
              items:[
                  {
                      xtype:'box',
                      html:'子券数量',
                      margin:'20px 0',
                      itemId:'userControllerText',
                      columnWidth:1
                  },
                  {
                      xtype:'textfield',
                      columnWidth: 1,
                      margin:'10px 0 0 0',
                      fieldStyle:'font-size:16px;',
                      emptyText:'1',
                      itemId:'subVoucherVal',
                      height:45,
                      value:1,
                      cls:'setHolderStyle'
                  }
              ]
          }
      ]
      me.callParent(arguments);
  },
  getData:function(){
      var me = this;
      var product = JE.ajax({
          url:'/je/shoppingmall/voucher/queryAllChargeProducts',
          params:''
      })
      var data = [];
      if(product.success){
          Ext.each(product.obj,function(item){
              data.push({
                  text:item.productName,
                  code:item.productId,
                  type:item.productPayType
              })
          })
      }
      return data
  },
  afterRender:function(){
      var me = this;
      me.callParent(arguments);
      if(me.active){
          me.getLayout().setActiveItem('subVoucher');
      }
  }
});
